interface Opening {
  name: string
  moves: string[]
  variations?: Opening[]
}

export function useOpeningBook() {
  const openings: Opening[] = [
    {
      name: "Ruy Lopez",
      moves: ["e4", "e5", "Nf3", "Nc6", "Bb5"],
      variations: [
        {
          name: "Berlin Defense",
          moves: ["e4", "e5", "Nf3", "Nc6", "Bb5", "Nf6"]
        },
        {
          name: "Morphy Defense",
          moves: ["e4", "e5", "Nf3", "Nc6", "Bb5", "a6"]
        }
      ]
    },
    {
      name: "Sicilian Defense",
      moves: ["e4", "c5"],
      variations: [
        {
          name: "Najdorf Variation",
          moves: ["e4", "c5", "Nf3", "d6", "d4", "cxd4", "Nxd4", "Nf6", "Nc3", "a6"]
        },
        {
          name: "Dragon Variation",
          moves: ["e4", "c5", "Nf3", "d6", "d4", "cxd4", "Nxd4", "Nf6", "Nc3", "g6"]
        }
      ]
    }
  ]

  const findOpening = (moves: string[]): Opening | null => {
    const moveStr = moves.join(" ")
    
    for (const opening of openings) {
      if (moveStr.startsWith(opening.moves.join(" "))) {
        return opening
      }
      
      if (opening.variations) {
        for (const variation of opening.variations) {
          if (moveStr.startsWith(variation.moves.join(" "))) {
            return variation
          }
        }
      }
    }
    
    return null
  }

  const getNextBookMove = (moves: string[]): string | null => {
    const opening = findOpening(moves)
    if (!opening) return null
    
    const nextMoveIndex = moves.length
    return opening.moves[nextMoveIndex] || null
  }

  return {
    findOpening,
    getNextBookMove
  }
} 